var static={
	area:[
			['1','华北'],
			['2','东北'],
			['3','华东'],
			['4','中南'],
			['5','西南'],
			['6','西北'],
			['7','港奥台'],
			['8','其他']
		 ],
	provinces: 
		[
			/*['1',['010','北京','BEIJING']],
			['1',['020','上海','SHANGHAI']],
			['1',['030','天津','TIANJIN']],
			['1',['040','重庆','CHONGQING']],*///测试完毕之后改成-1;
			['1',['010','北京','BEIJING'],['020','上海','SHANGHAI'],['040','重庆','CHONGQING']],
			['4',['050','广东省','GUANGDONG']],
			['3',['060','江苏省','JIANGSU']],
	    ],
	city:
		[
			['050',['050020','广州','GUANGZHOU'],['050030','潮州','CHAOZHOU'],
				   ['050040','东莞','DONGGUAN'],['050050','佛山','FOSHAN'],
				   ['050060','惠州','HUIZHOU'],['050070','清远','QINGYUAN'],
				   ['050080','汕头','SHANTOU'],['050090','深圳','SHENZHEN'],
				   ['050100','顺德','SHUNDE'],['050110','湛江','ZHANJIANG'],
				   ['050120','肇庆','ZHAOQING'],['050130','中山','ZHONGSHAN'],
				   ['050140','珠海','ZHUHAI']],
			['060',['060020','南京','NANJIN'],['060030','常熟','CHANGSHU'],
				   ['060040','常州','CHANGZHOU'],['060050','昆山','KUNSHAN'],
				   ['060060','连云港','LIANYUNGANG'],['060070','南通','NANTONG'],
				   ['060080','苏州','SUZHOU'],['060090','太仓','TAICANG'],
				   ['060100','无锡','WUXI'],['060110','徐州','XUZHOU'],
				   ['060120','扬州','YANGZHOU'],['060130','镇江','ZHENJIANG']],
		  
		],
	tradecategory:
		[
			['010','IT|互联网|通信|电子'],
			['020','金融|银行|保险']

		],
	trade:
		[
		 
		 	['010',['010010','计算机软件'],['010020','IT服务(系统/数据/维护)/多领域经营'],
				   ['010030','计算机硬件及网络设备'],
				   ['010040','互联网/电子商务'],['010050','网络游戏'],['010060','通信/电信/网络设备'],
				   ['010010','计算机软件'],['010020','IT服务(系统/数据/维护)/多领域经营'],
				   ['010030','计算机硬件及网络设备'],
				   ['010040','互联网/电子商务'],['010050','网络游戏'],['010060','通信/电信/网络设备'],
				   ['010070','电子技术/半导体/集成电路务']],
			['020',['020010','金融/投资/基金/证券/期货'],['020030','银行'],['020040','保险'],
				   ['010040','房地产|建筑建设|物业']]
		],
	jobcategory:
		[
		 	['010','销售类'],
			//['020','经营管理类'],
			['030','计算机·网络·技术类']
		],
	job:[
			['010',['010010','区域销售总监'],['010020','销售总监'],['010030','销售经理·销售主管'],['010040','区域销售经理'],
				   ['010050','渠道总监·分销总监'],['010060','渠道分销经理·渠道分销主管'],['010070','售前支持经理·售前支持主管'],
				   ['010080','售前支持工程师'],['010090','商务经理·商务主管'],['010100','业务拓展(BD)经理'],['010110','其他销售类']],
			['030',['010010','计算机1软件'],['010020','IT服务(系1统/数据/维护)/多领域经营'],
				   ['010030','计算机硬件1及网络设备'],
				   ['010040','互联网/电子商务'],['010050','网络游戏'],['010060','通信/电信/网络设备'],
				   ['010010','计算机软件'],['010020','IT服务(系统/数据/维护)/多领域经营'],
				   ['010030','计算机硬件及网络设备'],
				   ['010040','互联网/电子商务'],['010050','网络游戏'],['010060','通信/电信/网络设备'],
				   ['010070','电子技术/半导体/集成电路务']],
		]
};
$(function(){
	
	function Selinput(){
		this.config=Selinput._default;
		this.templates=Selinput._templates;
	};
	Selinput.prototype={
		mergeObject:function(_obj){
			var obj={};
            for(var attr in this.config){  
                obj[attr]=_obj[attr]||this.config[attr];  
            }
			switch(_obj["name"]){
				case "trade":
					obj["data"]=[static.tradecategory,static.trade];
					break;
				case "area":
					obj["data"]=[static.area,static.provinces,static.city];
					break;
				case "job":
					obj["data"]=[static.jobcategory,static.job];
					break;
			}
			return obj;
        },
		bindArea:function(){
			this.top=$(this.templates.areashow);
			this.result=this.top.find(".city");
			this.bindAreaRoot();
		},
		bindAreaRoot:function(){
			var arr=this.config.data[0],
				_temp=[];
				target=this.top.find("ul.area");
			for(var i in arr){
				_li=this.templates.parentli;
				_li=_li.replace(/#id#/,arr[i][0]);
				_li=_li.replace(/#name#/,arr[i][1]);
				_temp.push(_li);
			}
			var _temp_p=$(_temp.join(""));
			target.append(_temp_p);
			//加事件
			this.bindAreaparent(arr[0][0])
		},
		bindAreaparent:function(code){
			var arr=this.config.data[1],
				_temp=[];
				target=this.top.find("ul.province");
			for(var i in arr){
				if(arr[i][0]==code){
					var arr_select=[];
					for(var k=1;k<arr[i].length;k++){
						_li=this.templates.parentli;
						_li=_li.replace(/#id#/,arr[i][k][0]);
						_li=_li.replace(/#name#/,arr[i][k][1]);
						_temp.push(_li);
					}
					//target.children().remove();
					target.append($(_temp.join("")));
					break;
				}
			}
			this.bindAreachid("050");//暂时测试
		},
		bindAreachid:function(code){
			var arr=this.config.data[2],
				_temp=[];
				target=this.top.find("ul.city");
			for(var i in arr){
				if(arr[i][0]==code){
					var arr_select=[];
					for(var k=1;k<arr[i].length;k++){
						_li=this.templates.parentli;
						_li=_li.replace(/#id#/,arr[i][k][0]);
						_li=_li.replace(/#name#/,arr[i][k][1]);
						//_li=_li.replace(/#type#/,this.config.seltype);
						arr_select.push(_li);
					}
					//target.children().remove();
					target.append($(arr_select.join("")));
					break;
				}
			}
			//加事件
		},
		bindParentlist:function(){
			this.parent_index=0;
			if(this.config.name=="area"){
				this.bindArea();
				return;
			}
			this.top=$(this.templates.show);
			this.result=this.top.find(".cols:eq(1)");
			var	result = this.top.find(".selector_title"),
				p_List = this.top.find(".cols:first").find("ul"),
				cur_array=this.config.data[0],
				_temp=[],_li="";
			for(var i in cur_array){
				_li=this.templates.parentli;
				_li=_li.replace(/#id#/,cur_array[i][0]);
				_li=_li.replace(/#name#/,cur_array[i][1]);
				_temp.push(_li);
			}
			var _temp_p=$(_temp.join(""));
				_this=this;
			p_List.append(_temp_p);
			_temp_p.first().addClass("current");
			_temp_p.bind("click",function(){
				_temp_p.get(_this.parent_index).className="";
				$(this).addClass("current");
				_this.bindChildlist($(this).attr("code"));
				_this.parent_index=_temp_p.index($(this));
				
			});
			_this.bindChildlist(this.config.data[1][0][0]);
		},
		getData:function(){
			//把这里改成公用的,如果
			var _input=this.result.find("input"),
			 	 result=[];
			_input.each(function(){
					if($(this).is(":checked")){
						//var _temp=[$(this).val(),$(this).get(0).nextSibling.nodeValue];
						var _temp=$(this).val();
						result.push(_temp);
					}
				}
			 )
			return result;
		},
		//绑定list
		bindChildlist:function(code){
			var cur_array=this.config.data[1],
				target=this.result.find("ul"),
				_temp=[];
			for(var i in cur_array){
				if(cur_array[i][0]==code){
					var arr_select=[];
					for(var k=1;k<cur_array[i].length;k++){
						_list=this.templates.childli;
						_list=_list.replace(/#id#/,cur_array[i][k][0]);
						_list=_list.replace(/#name#/,cur_array[i][k][1]);
						_list=_list.replace(/#type#/,this.config.seltype);
						_temp.push(_list);
					}
					target.children().remove();
					target.append($(_temp.join("")));
					break;
				}
			}
		},
		//获取父name
		getParent_name:function(code){
			var cur_array=this.config.data[0];
			for(var i in cur_array){
				if(cur_array[i][0]==code){
					return cur_array[i][1];
				}
			}
		},
		//获取字name
		getChild_name:function(code){
			var cur_array=this.config.data[1],
			    _temp=[];
			for(var i in cur_array){
				if(cur_array[i][0]==code.substring(0,3)){
					_temp=cur_array[i];
					break;
				}
			}
			for(var k=1;k<_temp.length;k++){
				if(_temp[k][0]==code){
					return _temp[k][1];
					break;
				}
			}
		},
		//显示
		showDlg:function(obj){
			var _input=obj,
				_this=this;
				_this.bindParentlist();
			$.dialog({
				title:"请选择",
				lock:true,
				padding:"15px 15px 0px 15px",
				content:this.top.get(0),
				okValue:"确定",
				cancelValue:"取消",
				cancel:function(){
				},
				ok:function(){
					var sel_data=_this.getData();
					_input.val(sel_data.join(","));
					_this.edit_style(_input);
					
				}
			})
		},
		//编辑时候的样式
		edit_style:function(obj){
			if(obj.val()==""){
				return;
			}
			var list=obj.val().split(","),
				li='<li name="#result#">#name#<em></em></li>',
				_p= obj.parent(),
				result =_p.find(".pop_selection_result").empty().addClass("pop_selection_hide"),
				modify = _p.find(".pop_selection_modify").addClass("pop_selection_hide"),
				selection =_p.find(".pop_selection").addClass("pop_selection_hide");
			switch(list[0].length){
				case 3:
					for(var i in list){
						var _li=li.replace(/#result#/,list[i]),
							_p_name=this.getParent_name(list[i]);
						_li=_li.replace(/#name#/,_p_name);
						$(_li).appendTo(result);
					}
				break;
				case 6:
					for(var k in list){
						var _li=li.replace(/#result#/,list[k]),
							_p_name=this.getChild_name(list[k]);
						_li=_li.replace(/#name#/,_p_name);
						$(_li).appendTo(result);
					}
				break;
			}
			modify.removeClass("pop_selection_hide");
    		result.removeClass("pop_selection_hide");
			result.find("em").bind("click",function(){
				var index=result.find("em").index(this);
				list.splice(index,1);
				obj.val(list.join(","));
				$(this).parents("li").remove();
				if(obj.val()==""){
					modify.addClass("pop_selection_hide");
					result.addClass("pop_selection_hide");
					obj.parent().find(".pop_selection").removeClass("pop_selection_hide");
				}
				
			})
		}
	};
	Selinput._templates={
		select:'<span class="pop_selection input-text"><em class="icon_selection"><a href="javascript:;"></a></em></span>',
		modify:'<a class="pop_selection_modify" href="javascript:;">修改</a>',
		result:'<ul class="pop_selection_result input-text pop_selection_inline_block"></ul>',
		parentli:'<li code="#id#"><a href="javascript:;">#name#</a></li>',
		childli:'<li><label><input type="#type#" name="scjobs" value="#id#" class="inall">#name#</label></li>',
		show:'<div class="pop_cols_selector job-cols-list">'
+	      '<div class="selector_title">'
+	           '<strong></strong>'
+	           '<ul></ul>'
+	      '</div>'
+	      '<div class="col-list clearfix">'
+	          '<div class="cols"><h2>一级分类</h2><ul></ul></div>'
+	          '<div class="cols"><h2>二级分类</h2><ul></ul></div>'
+	     '</div>',
		areashow:'<div id="sw_area">'
+			'<ul class="area"></ul>'
+			'<ul class="province"></ul>'
+			'<ul class="city"></ul></div>'
	};
	Selinput._default={
		"seltype":"checkbox",
		"all":true,
		"maxlength":0,
		"data":[],
		"name":"-1"   //trade行业  area地区  //工种  
	};
	(function(fyx){
		fyx.fn.bindselect=function(obj){
				var	_obj=new Selinput,
					select=_obj.templates.select,
				    modify=_obj.templates.modify,
				    result=_obj.templates.result;
					_obj.config=_obj.mergeObject(obj);
			this.each(function(){
				var _this=$(this),
					_select=$(select).insertAfter(_this).width(_this.width()).bind("click",function(){_obj.showDlg(_this)}),
					_result=$(result).insertAfter(_select).addClass("pop_selection_hide"),
					_modify=$(modify).insertAfter(_result).addClass("pop_selection_hide").bind("click",function(){_obj.showDlg(_this)});
					_obj.edit_style(_this);////初始化样式*/
			});
			this.hide();
		}
	})(jQuery);
		   
});